home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / info / internals.info-5.z / internals.info-5
Encoding:
GNU Info File  |  1998-05-21  |  49.0 KB  |  1,003 lines

  1. This is Info file ../../info/internals.info, produced by Makeinfo
  2. version 1.68 from the input file internals.texi.
  3.  
  4.    Copyright (C) 1992 - 1996 Ben Wing.  Copyright (C) 1996, 1997 Sun
  5. Microsystems.  Copyright (C) 1994, 1995 Free Software Foundation.
  6. Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
  7.  
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.  
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that the
  14. entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.  
  17.    Permission is granted to copy and distribute translations of this
  18. manual into another language, under the above conditions for modified
  19. versions, except that this permission notice may be stated in a
  20. translation approved by the Foundation.
  21.  
  22.    Permission is granted to copy and distribute modified versions of
  23. this manual under the conditions for verbatim copying, provided also
  24. that the section entitled "GNU General Public License" is included
  25. exactly as in the original, and provided that the entire resulting
  26. derived work is distributed under the terms of a permission notice
  27. identical to this one.
  28.  
  29.    Permission is granted to copy and distribute translations of this
  30. manual into another language, under the above conditions for modified
  31. versions, except that the section entitled "GNU General Public License"
  32. may be included in a translation approved by the Free Software
  33. Foundation instead of in the original English.
  34.  
  35. 
  36. File: internals.info,  Node: Main Loop,  Next: Specifics of the Event Gathering Mechanism,  Prev: Introduction to Events,  Up: Events and the Event Loop
  37.  
  38. Main Loop
  39. =========
  40.  
  41.    The "command loop" is the top-level loop that the editor is always
  42. running.  It loops endlessly, calling `next-event' to retrieve an event
  43. and `dispatch-event' to execute it. `dispatch-event' does the
  44. appropriate thing with non-user events (process, timeout, magic, eval,
  45. mouse motion); this involves calling a Lisp handler function, redrawing
  46. a newly-exposed part of a frame, reading subprocess output, etc.  For
  47. user events, `dispatch-event' looks up the event in relevant keymaps or
  48. menubars; when a full key sequence or menubar selection is reached, the
  49. appropriate function is executed. `dispatch-event' may have to keep
  50. state across calls; this is done in the "command-builder" structure
  51. associated with each console (remember, there's usually only one
  52. console), and the engine that looks up keystrokes and constructs full
  53. key sequences is called the "command builder".  This is documented
  54. elsewhere.
  55.  
  56.    The guts of the command loop are in `command_loop_1()'.  This
  57. function doesn't catch errors, though - that's the job of
  58. `command_loop_2()', which is a condition-case (i.e. error-trapping)
  59. wrapper around `command_loop_1()'.  `command_loop_1()' never returns,
  60. but may get thrown out of.
  61.  
  62.    When an error occurs, `cmd_error()' is called, which usually invokes
  63. the Lisp error handler in `command-error'; however, a default error
  64. handler is provided if `command-error' is `nil' (e.g. during startup).
  65. The purpose of the error handler is simply to display the error message
  66. and do associated cleanup; it does not need to throw anywhere.  When
  67. the error handler finishes, the condition-case in `command_loop_2()'
  68. will finish and `command_loop_2()' will reinvoke `command_loop_1()'.
  69.  
  70.    `command_loop_2()' is invoked from three places: from
  71. `initial_command_loop()' (called from `main()' at the end of internal
  72. initialization), from the Lisp function `recursive-edit', and from
  73. `call_command_loop()'.
  74.  
  75.    `call_command_loop()' is called when a macro is started and when the
  76. minibuffer is entered; normal termination of the macro or minibuffer
  77. causes a throw out of the recursive command loop. (To
  78. `execute-kbd-macro' for macros and `exit' for minibuffers.  Note also
  79. that the low-level minibuffer-entering function,
  80. `read-minibuffer-internal', provides its own error handling and does
  81. not need `command_loop_2()''s error encapsulation; so it tells
  82. `call_command_loop()' to invoke `command_loop_1()' directly.)
  83.  
  84.    Note that both read-minibuffer-internal and recursive-edit set up a
  85. catch for `exit'; this is why `abort-recursive-edit', which throws to
  86. this catch, exits out of either one.
  87.  
  88.    `initial_command_loop()', called from `main()', sets up a catch for
  89. `top-level' when invoking `command_loop_2()', allowing functions to
  90. throw all the way to the top level if they really need to.  Before
  91. invoking `command_loop_2()', `initial_command_loop()' calls
  92. `top_level_1()', which handles all of the startup stuff (creating the
  93. initial frame, handling the command-line options, loading the user's
  94. `.emacs' file, etc.).  The function that actually does this is in Lisp
  95. and is pointed to by the variable `top-level'; normally this function is
  96. `normal-top-level'.  `top_level_1()' is just an error-handling wrapper
  97. similar to `command_loop_2()'.  Note also that `initial_command_loop()'
  98. sets up a catch for `top-level' when invoking `top_level_1()', just
  99. like when it invokes `command_loop_2()'.
  100.  
  101. 
  102. File: internals.info,  Node: Specifics of the Event Gathering Mechanism,  Next: Specifics About the Emacs Event,  Prev: Main Loop,  Up: Events and the Event Loop
  103.  
  104. Specifics of the Event Gathering Mechanism
  105. ==========================================
  106.  
  107.    Here is an approximate diagram of the collection processes at work
  108. in XEmacs, under TTY's (TTY's are simpler than X so we'll look at this
  109. first):
  110.  
  111.       asynch.      asynch.    asynch.   asynch.                [Collectors in
  112.      kbd events  kbd events   process   process                   the OS]
  113.            |         |         output    output
  114.            |         |           |         |
  115.            |         |           |         |      SIGINT,      [signal handlers
  116.            |         |           |         |      SIGQUIT,        in XEmacs]
  117.            V         V           V         V      SIGWINCH,
  118.           file      file        file      file    SIGALRM
  119.           desc.     desc.       desc.     desc.     |
  120.           (TTY)     (TTY)       (pipe)    (pipe)    |
  121.            |          |          |         |      fake    timeouts
  122.            |          |          |         |      file        |
  123.            |          |          |         |      desc.       |
  124.            |          |          |         |      (pipe)      |
  125.            |          |          |         |        |         |
  126.            |          |          |         |        |         |
  127.            |          |          |         |        |         |
  128.            V          V          V         V        V         V
  129.            ------>-----------<----------------<----------------
  130.                         |
  131.                         |
  132.                         |   [collected using select() in emacs_tty_next_event()
  133.                         |    and converted to the appropriate Emacs event]
  134.                         |
  135.                         |
  136.                         V              (above this line is TTY-specific)
  137.                       Emacs    ------------------------------------------------
  138.                       event    (below this line is the generic event mechanism)
  139.                         |
  140.                         |
  141.      was there      if not, call
  142.      a SIGINT?   emacs_tty_next_event()
  143.          |              |
  144.          |              |
  145.          |              |
  146.          V              V
  147.          --->-------<----
  148.                 |
  149.                 |        [collected in event_stream_next_event();
  150.                 |         SIGINT is converted using maybe_read_quit_event()]
  151.                 V
  152.               Emacs
  153.               event
  154.                 |
  155.                 \---->------>----- maybe_kbd_translate() ---->---\
  156.                                                                  |
  157.                                                                  |
  158.                                                                  |
  159.           command event queue                                    |
  160.                                                       if not from command
  161.        (contains events that were                     event queue, call
  162.        read earlier but not processed,                event_stream_next_event()
  163.        typically when waiting in a                               |
  164.        sit-for, sleep-for, etc. for                              |
  165.       a particular event to be received)                         |
  166.                     |                                            |
  167.                     |                                            |
  168.                     V                                            V
  169.                     ---->------------------------------------<----
  170.                                                     |
  171.                                                     |   [collected in
  172.                                                     |    next_event_internal()]
  173.                                                     |
  174.       unread-     unread-       event from          |
  175.       command-    command-       keyboard       else, call
  176.       events      event           macro      next_event_internal()
  177.         |           |               |               |
  178.         |           |               |               |
  179.         |           |               |               |
  180.         V           V               V               V
  181.         --------->----------------------<------------
  182.                           |
  183.                           |      [collected in `next-event', which may loop
  184.                           |       more than once if the event it gets is on
  185.                           |       a dead frame, device, etc.]
  186.                           |
  187.                           |
  188.                           V
  189.                  feed into top-level event loop,
  190.                  which repeatedly calls `next-event'
  191.                  and then dispatches the event
  192.                  using `dispatch-event'
  193.  
  194.    Notice the separation between TTY-specific and generic event
  195. mechanism.  When using the Xt-based event loop, the TTY-specific stuff
  196. is replaced but the rest stays the same.
  197.  
  198.    It's also important to realize that only one different kind of
  199. system-specific event loop can be operating at a time, and must be able
  200. to receive all kinds of events simultaneously.  For the two existing
  201. event loops (implemented in `event-tty.c' and `event-Xt.c',
  202. respectively), the TTY event loop *only* handles TTY consoles, while
  203. the Xt event loop handles *both* TTY and X consoles.  This situation is
  204. different from all of the output handlers, where you simply have one
  205. per console type.
  206.  
  207.    Here's the Xt Event Loop Diagram (notice that below a certain point,
  208. it's the same as the above diagram):
  209.  
  210.      asynch. asynch. asynch. asynch.                 [Collectors in
  211.       kbd     kbd    process process                    the OS]
  212.      events  events  output  output
  213.        |       |       |       |
  214.        |       |       |       |     asynch. asynch.   [Collectors in the
  215.        |       |       |       |       X        X       OS and X Window System]
  216.        |       |       |       |     events  events
  217.        |       |       |       |       |        |
  218.        |       |       |       |       |        |
  219.        |       |       |       |       |        |    SIGINT,   [signal handlers
  220.        |       |       |       |       |        |    SIGQUIT,     in XEmacs]
  221.        |       |       |       |       |        |    SIGWINCH,
  222.        |       |       |       |       |        |    SIGALRM
  223.        |       |       |       |       |        |       |
  224.        |       |       |       |       |        |       |
  225.        |       |       |       |       |        |       |      timeouts
  226.        |       |       |       |       |        |       |          |
  227.        |       |       |       |       |        |       |          |
  228.        |       |       |       |       |        |       V          |
  229.        V       V       V       V       V        V      fake        |
  230.       file    file    file    file    file     file    file        |
  231.       desc.   desc.   desc.   desc.   desc.    desc.   desc.       |
  232.       (TTY)   (TTY)   (pipe)  (pipe) (socket) (socket) (pipe)      |
  233.        |       |       |       |       |        |       |          |
  234.        |       |       |       |       |        |       |          |
  235.        |       |       |       |       |        |       |          |
  236.        V       V       V       V       V        V       V          V
  237.        --->----------------------------------------<---------<------
  238.             |              |               |
  239.             |              |               |   [collected using select() in
  240.             |              |               |   _XtWaitForSomething(), called
  241.             |              |               |   from XtAppProcessEvent(), called
  242.             |              |               |   in emacs_Xt_next_event();
  243.             |              |               |   dispatched to various callbacks]
  244.             |              |               |
  245.             |              |               |
  246.        emacs_Xt_        p_s_callback(),    |   [popup_selection_callback]
  247.        event_handler()  x_u_v_s_callback(),|   [x_update_vertical_scrollbar_
  248.             |           x_u_h_s_callback(),|    callback]
  249.             |           search_callback()  |   [x_update_horizontal_scrollbar_
  250.             |              |               |    callback]
  251.             |              |               |
  252.             |              |               |
  253.        enqueue_Xt_       signal_special_   |
  254.        dispatch_event()  Xt_user_event()   |
  255.        [maybe multiple     |               |
  256.         times, maybe 0     |               |
  257.         times]             |               |
  258.             |            enqueue_Xt_       |
  259.             |            dispatch_event()  |
  260.             |              |               |
  261.             |              |               |
  262.             V              V               |
  263.             -->----------<--               |
  264.                    |                       |
  265.                    |                       |
  266.                 dispatch             Xt_what_callback()
  267.                 event                  sets flags
  268.                 queue                      |
  269.                    |                       |
  270.                    |                       |
  271.                    |                       |
  272.                    |                       |
  273.                    ---->-----------<--------
  274.                         |
  275.                         |
  276.                         |     [collected and converted as appropriate in
  277.                         |            emacs_Xt_next_event()]
  278.                         |
  279.                         |
  280.                         V              (above this line is Xt-specific)
  281.                       Emacs    ------------------------------------------------
  282.                       event    (below this line is the generic event mechanism)
  283.                         |
  284.                         |
  285.      was there      if not, call
  286.      a SIGINT?   emacs_Xt_next_event()
  287.          |              |
  288.          |              |
  289.          |              |
  290.          V              V
  291.          --->-------<----
  292.                 |
  293.                 |        [collected in event_stream_next_event();
  294.                 |         SIGINT is converted using maybe_read_quit_event()]
  295.                 V
  296.               Emacs
  297.               event
  298.                 |
  299.                 \---->------>----- maybe_kbd_translate() -->-----\
  300.                                                                  |
  301.                                                                  |
  302.                                                                  |
  303.           command event queue                                    |
  304.                                                       if not from command
  305.        (contains events that were                     event queue, call
  306.        read earlier but not processed,                event_stream_next_event()
  307.        typically when waiting in a                               |
  308.        sit-for, sleep-for, etc. for                              |
  309.       a particular event to be received)                         |
  310.                     |                                            |
  311.                     |                                            |
  312.                     V                                            V
  313.                     ---->----------------------------------<------
  314.                                                     |
  315.                                                     |   [collected in
  316.                                                     |    next_event_internal()]
  317.                                                     |
  318.       unread-     unread-       event from          |
  319.       command-    command-       keyboard       else, call
  320.       events      event           macro      next_event_internal()
  321.         |           |               |               |
  322.         |           |               |               |
  323.         |           |               |               |
  324.         V           V               V               V
  325.         --------->----------------------<------------
  326.                           |
  327.                           |      [collected in `next-event', which may loop
  328.                           |       more than once if the event it gets is on
  329.                           |       a dead frame, device, etc.]
  330.                           |
  331.                           |
  332.                           V
  333.                  feed into top-level event loop,
  334.                  which repeatedly calls `next-event'
  335.                  and then dispatches the event
  336.                  using `dispatch-event'
  337.  
  338. 
  339. File: internals.info,  Node: Specifics About the Emacs Event,  Next: The Event Stream Callback Routines,  Prev: Specifics of the Event Gathering Mechanism,  Up: Events and the Event Loop
  340.  
  341. Specifics About the Emacs Event
  342. ===============================
  343.  
  344. 
  345. File: internals.info,  Node: The Event Stream Callback Routines,  Next: Other Event Loop Functions,  Prev: Specifics About the Emacs Event,  Up: Events and the Event Loop
  346.  
  347. The Event Stream Callback Routines
  348. ==================================
  349.  
  350. 
  351. File: internals.info,  Node: Other Event Loop Functions,  Next: Converting Events,  Prev: The Event Stream Callback Routines,  Up: Events and the Event Loop
  352.  
  353. Other Event Loop Functions
  354. ==========================
  355.  
  356.    `detect_input_pending()' and `input-pending-p' look for input by
  357. calling `event_stream->event_pending_p' and looking in
  358. `[V]unread-command-event' and the `command_event_queue' (they do not
  359. check for an executing keyboard macro, though).
  360.  
  361.    `discard-input' cancels any command events pending (and any keyboard
  362. macros currently executing), and puts the others onto the
  363. `command_event_queue'.  There is a comment about a "race condition",
  364. which is not a good sign.
  365.  
  366.    `next-command-event' and `read-char' are higher-level interfaces to
  367. `next-event'.  `next-command-event' gets the next "command" event (i.e.
  368. keypress, mouse event, menu selection, or scrollbar action), calling
  369. `dispatch-event' on any others.  `read-char' calls `next-command-event'
  370. and uses `event_to_character()' to return the character equivalent.
  371. With the right kind of input method support, it is possible for
  372. (read-char) to return a Kanji character.
  373.  
  374. 
  375. File: internals.info,  Node: Converting Events,  Next: Dispatching Events; The Command Builder,  Prev: Other Event Loop Functions,  Up: Events and the Event Loop
  376.  
  377. Converting Events
  378. =================
  379.  
  380.    `character_to_event()', `event_to_character()',
  381. `event-to-character', and `character-to-event' convert between
  382. characters and keypress events corresponding to the characters.  If the
  383. event was not a keypress, `event_to_character()' returns -1 and
  384. `event-to-character' returns `nil'.  These functions convert between
  385. character representation and the split-up event representation (keysym
  386. plus mod keys).
  387.  
  388. 
  389. File: internals.info,  Node: Dispatching Events; The Command Builder,  Prev: Converting Events,  Up: Events and the Event Loop
  390.  
  391. Dispatching Events; The Command Builder
  392. =======================================
  393.  
  394.    Not yet documented.
  395.  
  396. 
  397. File: internals.info,  Node: Evaluation; Stack Frames; Bindings,  Next: Symbols and Variables,  Prev: Events and the Event Loop,  Up: Top
  398.  
  399. Evaluation; Stack Frames; Bindings
  400. **********************************
  401.  
  402. * Menu:
  403.  
  404. * Evaluation::
  405. * Dynamic Binding; The specbinding Stack; Unwind-Protects::
  406. * Simple Special Forms::
  407. * Catch and Throw::
  408.  
  409. 
  410. File: internals.info,  Node: Evaluation,  Next: Dynamic Binding; The specbinding Stack; Unwind-Protects,  Up: Evaluation; Stack Frames; Bindings
  411.  
  412. Evaluation
  413. ==========
  414.  
  415.    `Feval()' evaluates the form (a Lisp object) that is passed to it.
  416. Note that evaluation is only non-trivial for two types of objects:
  417. symbols and conses.  Under normal circumstances (i.e. not mocklisp) a
  418. symbol is evaluated simply by calling symbol-value on it and returning
  419. the value.
  420.  
  421.    Evaluating a cons means calling a function.  First, `eval' checks to
  422. see if garbage-collection is necessary, and calls `Fgarbage_collect()'
  423. if so.  It then increases the evaluation depth by 1 (`lisp_eval_depth',
  424. which is always less than `max_lisp_eval_depth') and adds an element to
  425. the linked list of `struct backtrace''s (`backtrace_list').  Each such
  426. structure contains a pointer to the function being called plus a list
  427. of the function's arguments.  Originally these values are stored
  428. unevalled, and as they are evaluated, the backtrace structure is
  429. updated.  Garbage collection pays attention to the objects pointed to
  430. in the backtrace structures (garbage collection might happen while a
  431. function is being called or while an argument is being evaluated, and
  432. there could easily be no other references to the arguments in the
  433. argument list; once an argument is evaluated, however, the unevalled
  434. version is not needed by eval, and so the backtrace structure is
  435. changed).
  436.  
  437.    At this point, the function to be called is determined by looking at
  438. the car of the cons (if this is a symbol, its function definition is
  439. retrieved and the process repeated).  The function should then consist
  440. of either a `Lisp_Subr' (built-in function), a `Lisp_Compiled_Function'
  441. object, or a cons whose car is the symbol `autoload', `macro',
  442. `lambda', or `mocklisp'.
  443.  
  444.    If the function is a `Lisp_Subr', the lisp object points to a
  445. `struct Lisp_Subr' (created by `DEFUN()'), which contains a pointer to
  446. the C function, a minimum and maximum number of arguments (possibly the
  447. special constants `MANY' or `UNEVALLED'), a pointer to the symbol
  448. referring to that subr, and a couple of other things.  If the subr
  449. wants its arguments `UNEVALLED', they are passed raw as a list.
  450. Otherwise, an array of evaluated arguments is created and put into the
  451. backtrace structure, and either passed whole (`MANY') or each argument
  452. is passed as a C argument.
  453.  
  454.    If the function is a `Lisp_Compiled_Function' object or a lambda,
  455. `apply_lambda()' is called.  If the function is a macro, [..... fill
  456. in] is done.  If the function is an autoload, `do_autoload()' is called
  457. to load the definition and then eval starts over [explain this more].
  458. If the function is a mocklisp, `ml_apply()' is called.
  459.  
  460.    When `Feval' exits, the evaluation depth is reduced by one, the
  461. debugger is called if appropriate, and the current backtrace structure
  462. is removed from the list.
  463.  
  464.    `apply_lambda()' is passed a function, a list of arguments, and a
  465. flag indicating whether to evaluate the arguments.  It creates an array
  466. of (possibly) evaluated arguments and fixes up the backtrace structure,
  467. just like eval does.  Then it calls `funcall_lambda()'.
  468.  
  469.    `funcall_lambda()' goes through the formal arguments to the function
  470. and binds them to the actual arguments, checking for `&rest' and
  471. `&optional' symbols in the formal arguments and making sure the number
  472. of actual arguments is correct.  Then either `progn' or `byte-code' is
  473. called to actually execute the body and return a value.
  474.  
  475.    `Ffuncall()' implements Lisp `funcall'.  `(funcall fun x1 x2 x3
  476. ...)' is equivalent to `(eval (list fun (quote x1) (quote x2) (quote
  477. x3) ...))'.  `Ffuncall()' contains its own code to do the evaluation,
  478. however, and is almost identical to eval.
  479.  
  480.    `Fapply()' implements Lisp `apply', which is very similar to funcall
  481. except that if the last argument is a list, the result is the same as
  482. if each of the arguments in the list had been passed separately.
  483. `Fapply()' does some business to expand the last argument if it's a
  484. list, then calls `Ffuncall()' to do the work.
  485.  
  486.    `apply1()', `call0()', `call1()', `call2()', and `call3()' call a
  487. function, passing it the argument(s) given (the arguments are given as
  488. separate C arguments rather than being passed as an array).  `apply1()'
  489. uses `apply' while the others use `funcall'.
  490.  
  491. 
  492. File: internals.info,  Node: Dynamic Binding; The specbinding Stack; Unwind-Protects,  Next: Simple Special Forms,  Prev: Evaluation,  Up: Evaluation; Stack Frames; Bindings
  493.  
  494. Dynamic Binding; The specbinding Stack; Unwind-Protects
  495. =======================================================
  496.  
  497.      struct specbinding
  498.      {
  499.        Lisp_Object symbol, old_value;
  500.        Lisp_Object (*func) (Lisp_Object); /* for unwind-protect */
  501.      };
  502.  
  503.    `struct specbinding' is used for local-variable bindings and
  504. unwind-protects.  `specpdl' holds an array of `struct specbinding''s,
  505. `specpdl_ptr' points to the beginning of the free bindings in the
  506. array, `specpdl_size' specifies the total number of binding slots in
  507. the array, and `max_specpdl_size' specifies the maximum number of
  508. bindings the array can be expanded to hold.  `grow_specpdl()' increases
  509. the size of the specpdl array, multiplying its size by 2 but never
  510. exceeding max_specpdl_size (except that if this number is less than
  511. 400, it is first set to 400).
  512.  
  513.    `specbind()' binds a symbol to a value and is used for local
  514. variables and `let' forms.  The symbol and its old value (which might
  515. be `Qunbound', indicating no prior value) are recorded in the specpdl
  516. array, and `specpdl_size' is increased by 1.
  517.  
  518.    `record_unwind_protect()' implements an "unwind-protect", which,
  519. when placed around a section of code, ensures that some specified
  520. cleanup routine will be executed even if the code exits abnormally
  521. (e.g. through a `throw' or quit).  `record_unwind_protect()' simply
  522. adds a new specbinding to the specpdl array and stores the appropriate
  523. information in it.  The cleanup routine can either be a C function,
  524. which is stored in the `func' field, or a `progn' form, which is stored
  525. in the `old_value' field.
  526.  
  527.    `unbind_to()' removes specbindings from the specpdl array until the
  528. specified position is reached.  Each specbinding can be one of three
  529. types:
  530.  
  531.   1. an unwind-protect with a C cleanup function (`func' is not 0, and
  532.      `old_value' holds an argument to be passed to the function);
  533.  
  534.   2. an unwind-protect with a Lisp form (`func' is 0, `symbol' is
  535.      `nil', and `old_value' holds the form to be executed with
  536.      `Fprogn()'); or
  537.  
  538.   3. a local-variable binding (`func' is 0, `symbol' is not `nil', and
  539.      `old_value' holds the old value, which is stored as the symbol's
  540.      value).
  541.  
  542. 
  543. File: internals.info,  Node: Simple Special Forms,  Next: Catch and Throw,  Prev: Dynamic Binding; The specbinding Stack; Unwind-Protects,  Up: Evaluation; Stack Frames; Bindings
  544.  
  545. Simple Special Forms
  546. ====================
  547.  
  548.    `or', `and', `if', `cond', `progn', `prog1', `prog2', `setq',
  549. `quote', `function', `let*', `let', `while'
  550.  
  551.    All of these are very simple and work as expected, calling `Feval()'
  552. or `Fprogn()' as necessary and (in the case of `let' and `let*') using
  553. `specbind()' to create bindings and `unbind_to()' to undo the bindings
  554. when finished.  Note that these functions do a lot of `GCPRO'ing to
  555. protect their arguments from garbage collection because they call
  556. `Feval()' (*note Garbage Collection::.).
  557.  
  558. 
  559. File: internals.info,  Node: Catch and Throw,  Prev: Simple Special Forms,  Up: Evaluation; Stack Frames; Bindings
  560.  
  561. Catch and Throw
  562. ===============
  563.  
  564.      struct catchtag
  565.      {
  566.        Lisp_Object tag;
  567.        Lisp_Object val;
  568.        struct catchtag *next;
  569.        struct gcpro *gcpro;
  570.        jmp_buf jmp;
  571.        struct backtrace *backlist;
  572.        int lisp_eval_depth;
  573.        int pdlcount;
  574.      };
  575.  
  576.    `catch' is a Lisp function that places a catch around a body of
  577. code.  A catch is a means of non-local exit from the code.  When a catch
  578. is created, a tag is specified, and executing a `throw' to this tag
  579. will exit from the body of code caught with this tag, and its value will
  580. be the value given in the call to `throw'.  If there is no such call,
  581. the code will be executed normally.
  582.  
  583.    Information pertaining to a catch is held in a `struct catchtag',
  584. which is placed at the head of a linked list pointed to by `catchlist'.
  585. `internal_catch()' is passed a C function to call (`Fprogn()' when
  586. Lisp `catch' is called) and arguments to give it, and places a catch
  587. around the function.  Each `struct catchtag' is held in the stack frame
  588. of the `internal_catch()' instance that created the catch.
  589.  
  590.    `internal_catch()' is fairly straightforward.  It stores into the
  591. `struct catchtag' the tag name and the current values of
  592. `backtrace_list', `lisp_eval_depth', `gcprolist', and the offset into
  593. the specpdl array, sets a jump point with `_setjmp()' (storing the jump
  594. point into the `struct catchtag'), and calls the function.  Control
  595. will return to `internal_catch()' either when the function exits
  596. normally or through a `_longjmp()' to this jump point.  In the latter
  597. case, `throw' will store the value to be returned into the `struct
  598. catchtag' before jumping.  When it's done, `internal_catch()' removes
  599. the `struct catchtag' from the catchlist and returns the proper value.
  600.  
  601.    `Fthrow()' goes up through the catchlist until it finds one with a
  602. matching tag.  It then calls `unbind_catch()' to restore everything to
  603. what it was when the appropriate catch was set, stores the return value
  604. in the `struct catchtag', and jumps (with `_longjmp()') to its jump
  605. point.
  606.  
  607.    `unbind_catch()' removes all catches from the catchlist until it
  608. finds the correct one.  Some of the catches might have been placed for
  609. error-trapping, and if so, the appropriate entries on the handlerlist
  610. must be removed (see "errors").  `unbind_catch()' also restores the
  611. values of `gcprolist', `backtrace_list', and `lisp_eval', and calls
  612. `unbind_to()' to undo any specbindings created since the catch.
  613.  
  614. 
  615. File: internals.info,  Node: Symbols and Variables,  Next: Buffers and Textual Representation,  Prev: Evaluation; Stack Frames; Bindings,  Up: Top
  616.  
  617. Symbols and Variables
  618. *********************
  619.  
  620. * Menu:
  621.  
  622. * Introduction to Symbols::
  623. * Obarrays::
  624. * Symbol Values::
  625.  
  626. 
  627. File: internals.info,  Node: Introduction to Symbols,  Next: Obarrays,  Up: Symbols and Variables
  628.  
  629. Introduction to Symbols
  630. =======================
  631.  
  632.    A symbol is basically just an object with four fields: a name (a
  633. string), a value (some Lisp object), a function (some Lisp object), and
  634. a property list (usually a list of alternating keyword/value pairs).
  635. What makes symbols special is that there is usually only one symbol with
  636. a given name, and the symbol is referred to by name.  This makes a
  637. symbol a convenient way of calling up data by name, i.e. of implementing
  638. variables. (The variable's value is stored in the "value slot".)
  639. Similarly, functions are referenced by name, and the definition of the
  640. function is stored in a symbol's "function slot".  This means that
  641. there can be a distinct function and variable with the same name.  The
  642. property list is used as a more general mechanism of associating
  643. additional values with particular names, and once again the namespace is
  644. independent of the function and variable namespaces.
  645.  
  646. 
  647. File: internals.info,  Node: Obarrays,  Next: Symbol Values,  Prev: Introduction to Symbols,  Up: Symbols and Variables
  648.  
  649. Obarrays
  650. ========
  651.  
  652.    The identity of symbols with their names is accomplished through a
  653. structure called an obarray, which is just a poorly-implemented hash
  654. table mapping from strings to symbols whose name is that string. (I say
  655. "poorly implemented" because an obarray appears in Lisp as a vector
  656. with some hidden fields rather than as its own opaque type.  This is an
  657. Emacs Lisp artifact that should be fixed.)
  658.  
  659.    Obarrays are implemented as a vector of some fixed size (which should
  660. be a prime for best results), where each "bucket" of the vector
  661. contains one or more symbols, threaded through a hidden `next' field in
  662. the symbol.  Lookup of a symbol in an obarray, and adding a symbol to
  663. an obarray, is accomplished through standard hash-table techniques.
  664.  
  665.    The standard Lisp function for working with symbols and obarrays is
  666. `intern'.  This looks up a symbol in an obarray given its name; if it's
  667. not found, a new symbol is automatically created with the specified
  668. name, added to the obarray, and returned.  This is what happens when the
  669. Lisp reader encounters a symbol (or more precisely, encounters the name
  670. of a symbol) in some text that it is reading.  There is a standard
  671. obarray called `obarray' that is used for this purpose, although the
  672. Lisp programmer is free to create his own obarrays and `intern' symbols
  673. in them.
  674.  
  675.    Note that, once a symbol is in an obarray, it stays there until
  676. something is done about it, and the standard obarray `obarray' always
  677. stays around, so once you use any particular variable name, a
  678. corresponding symbol will stay around in `obarray' until you exit
  679. XEmacs.
  680.  
  681.    Note that `obarray' itself is a variable, and as such there is a
  682. symbol in `obarray' whose name is `"obarray"' and which contains
  683. `obarray' as its value.
  684.  
  685.    Note also that this call to `intern' occurs only when in the Lisp
  686. reader, not when the code is executed (at which point the symbol is
  687. already around, stored as such in the definition of the function).
  688.  
  689.    You can create your own obarray using `make-vector' (this is
  690. horrible but is an artifact) and intern symbols into that obarray.
  691. Doing that will result in two or more symbols with the same name.
  692. However, at most one of these symbols is in the standard `obarray': You
  693. cannot have two symbols of the same name in any particular obarray.
  694. Note that you cannot add a symbol to an obarray in any fashion other
  695. than using `intern': i.e. you can't take an existing symbol and put it
  696. in an existing obarray.  Nor can you change the name of an existing
  697. symbol. (Since obarrays are vectors, you can violate the consistency of
  698. things by storing directly into the vector, but let's ignore that
  699. possibility.)
  700.  
  701.    Usually symbols are created by `intern', but if you really want, you
  702. can explicitly create a symbol using `make-symbol', giving it some
  703. name.  The resulting symbol is not in any obarray (i.e. it is
  704. "uninterned"), and you can't add it to any obarray.  Therefore its
  705. primary purpose is as a symbol to use in macros to avoid namespace
  706. pollution.  It can also be used as a carrier of information, but cons
  707. cells could probably be used just as well.
  708.  
  709.    You can also use `intern-soft' to look up a symbol but not create a
  710. new one, and `unintern' to remove a symbol from an obarray.  This
  711. returns the removed symbol. (Remember: You can't put the symbol back
  712. into any obarray.) Finally, `mapatoms' maps over all of the symbols in
  713. an obarray.
  714.  
  715. 
  716. File: internals.info,  Node: Symbol Values,  Prev: Obarrays,  Up: Symbols and Variables
  717.  
  718. Symbol Values
  719. =============
  720.  
  721.    The value field of a symbol normally contains a Lisp object.
  722. However, a symbol can be "unbound", meaning that it logically has no
  723. value.  This is internally indicated by storing a special Lisp object,
  724. called "the unbound marker" and stored in the global variable
  725. `Qunbound'.  The unbound marker is of a special Lisp object type called
  726. "symbol-value-magic".  It is impossible for the Lisp programmer to
  727. directly create or access any object of this type.
  728.  
  729.    *You must not let any "symbol-value-magic" object escape to the Lisp
  730. level.*  Printing any of these objects will cause the message `INTERNAL
  731. EMACS BUG' to appear as part of the print representation.  (You may see
  732. this normally when you call `debug_print()' from the debugger on a Lisp
  733. object.) If you let one of these objects escape to the Lisp level, you
  734. will violate a number of assumptions contained in the C code and make
  735. the unbound marker not function right.
  736.  
  737.    When a symbol is created, its value field (and function field) are
  738. set to `Qunbound'.  The Lisp programmer can restore these conditions
  739. later using `makunbound' or `fmakunbound', and can query to see whether
  740. the value of function fields are "bound" (i.e. have a value other than
  741. `Qunbound') using `boundp' and `fboundp'.  The fields are set to a
  742. normal Lisp object using `set' (or `setq') and `fset'.
  743.  
  744.    Other symbol-value-magic objects are used as special markers to
  745. indicate variables that have non-normal properties.  This includes any
  746. variables that are tied into C variables (setting the variable magically
  747. sets some global variable in the C code, and likewise for retrieving the
  748. variable's value), variables that magically tie into slots in the
  749. current buffer, variables that are buffer-local, etc.  The
  750. symbol-value-magic object is stored in the value cell in place of a
  751. normal object, and the code to retrieve a symbol's value (i.e.
  752. `symbol-value') knows how to do special things with them.  This means
  753. that you should not just fetch the value cell directly if you want a
  754. symbol's value.
  755.  
  756.    The exact workings of this are rather complex and involved and are
  757. well-documented in comments in `buffer.c', `symbols.c', and `lisp.h'.
  758.  
  759. 
  760. File: internals.info,  Node: Buffers and Textual Representation,  Next: MULE Character Sets and Encodings,  Prev: Symbols and Variables,  Up: Top
  761.  
  762. Buffers and Textual Representation
  763. **********************************
  764.  
  765. * Menu:
  766.  
  767. * Introduction to Buffers::     A buffer holds a block of text such as a file.
  768. * The Text in a Buffer::        Representation of the text in a buffer.
  769. * Buffer Lists::                Keeping track of all buffers.
  770. * Markers and Extents::         Tagging locations within a buffer.
  771. * Bufbytes and Emchars::        Representation of individual characters.
  772. * The Buffer Object::           The Lisp object corresponding to a buffer.
  773.  
  774. 
  775. File: internals.info,  Node: Introduction to Buffers,  Next: The Text in a Buffer,  Up: Buffers and Textual Representation
  776.  
  777. Introduction to Buffers
  778. =======================
  779.  
  780.    A buffer is logically just a Lisp object that holds some text.  In
  781. this, it is like a string, but a buffer is optimized for frequent
  782. insertion and deletion, while a string is not.  Furthermore:
  783.  
  784.   1. Buffers are "permanent" objects, i.e. once you create them, they
  785.      remain around, and need to be explicitly deleted before they go
  786.      away.
  787.  
  788.   2. Each buffer has a unique name, which is a string.  Buffers are
  789.      normally referred to by name.  In this respect, they are like
  790.      symbols.
  791.  
  792.   3. Buffers have a default insertion position, called "point".
  793.      Inserting text (unless you explicitly give a position) goes at
  794.      point, and moves point forward past the text.  This is what is
  795.      going on when you type text into Emacs.
  796.  
  797.   4. Buffers have lots of extra properties associated with them.
  798.  
  799.   5. Buffers can be "displayed".  What this means is that there exist a
  800.      number of "windows", which are objects that correspond to some
  801.      visible section of your display, and each window has an associated
  802.      buffer, and the current contents of the buffer are shown in that
  803.      section of the display.  The redisplay mechanism (which takes care
  804.      of doing this) knows how to look at the text of a buffer and come
  805.      up with some reasonable way of displaying this.  Many of the
  806.      properties of a buffer control how the buffer's text is displayed.
  807.  
  808.   6. One buffer is distinguished and called the "current buffer".  It is
  809.      stored in the variable `current_buffer'.  Buffer operations operate
  810.      on this buffer by default.  When you are typing text into a
  811.      buffer, the buffer you are typing into is always `current_buffer'.
  812.      Switching to a different window changes the current buffer.  Note
  813.      that Lisp code can temporarily change the current buffer using
  814.      `set-buffer' (often enclosed in a `save-excursion' so that the
  815.      former current buffer gets restored when the code is finished).
  816.      However, calling `set-buffer' will NOT cause a permanent change in
  817.      the current buffer.  The reason for this is that the top-level
  818.      event loop sets `current_buffer' to the buffer of the selected
  819.      window, each time it finishes executing a user command.
  820.  
  821.    Make sure you understand the distinction between "current buffer"
  822. and "buffer of the selected window", and the distinction between
  823. "point" of the current buffer and "window-point" of the selected
  824. window. (This latter distinction is explained in detail in the section
  825. on windows.)
  826.  
  827. 
  828. File: internals.info,  Node: The Text in a Buffer,  Next: Buffer Lists,  Prev: Introduction to Buffers,  Up: Buffers and Textual Representation
  829.  
  830. The Text in a Buffer
  831. ====================
  832.  
  833.    The text in a buffer consists of a sequence of zero or more
  834. characters.  A "character" is an integer that logically represents a
  835. letter, number, space, or other unit of text.  Most of the characters
  836. that you will typically encounter belong to the ASCII set of characters,
  837. but there are also characters for various sorts of accented letters,
  838. special symbols, Chinese and Japanese ideograms (i.e. Kanji, Katakana,
  839. etc.), Cyrillic and Greek letters, etc.  The actual number of possible
  840. characters is quite large.
  841.  
  842.    For now, we can view a character as some non-negative integer that
  843. has some shape that defines how it typically appears (e.g. as an
  844. uppercase A). (The exact way in which a character appears depends on the
  845. font used to display the character.) The internal type of characters in
  846. the C code is an `Emchar'; this is just an `int', but using a symbolic
  847. type makes the code clearer.
  848.  
  849.    Between every character in a buffer is a "buffer position" or
  850. "character position".  We can speak of the character before or after a
  851. particular buffer position, and when you insert a character at a
  852. particular position, all characters after that position end up at new
  853. positions.  When we speak of the character "at" a position, we really
  854. mean the character after the position.  (This schizophrenia between a
  855. buffer position being "between" a character and "on" a character is
  856. rampant in Emacs.)
  857.  
  858.    Buffer positions are numbered starting at 1.  This means that
  859. position 1 is before the first character, and position 0 is not valid.
  860. If there are N characters in a buffer, then buffer position N+1 is
  861. after the last one, and position N+2 is not valid.
  862.  
  863.    The internal makeup of the Emchar integer varies depending on whether
  864. we have compiled with MULE support.  If not, the Emchar integer is an
  865. 8-bit integer with possible values from 0 - 255.  0 - 127 are the
  866. standard ASCII characters, while 128 - 255 are the characters from the
  867. ISO-8859-1 character set.  If we have compiled with MULE support, an
  868. Emchar is a 19-bit integer, with the various bits having meanings
  869. according to a complex scheme that will be detailed later.  The
  870. characters numbered 0 - 255 still have the same meanings as for the
  871. non-MULE case, though.
  872.  
  873.    Internally, the text in a buffer is represented in a fairly simple
  874. fashion: as a contiguous array of bytes, with a "gap" of some size in
  875. the middle.  Although the gap is of some substantial size in bytes,
  876. there is no text contained within it: From the perspective of the text
  877. in the buffer, it does not exist.  The gap logically sits at some buffer
  878. position, between two characters (or possibly at the beginning or end of
  879. the buffer).  Insertion of text in a buffer at a particular position is
  880. always accomplished by first moving the gap to that position (i.e.
  881. through some block moving of text), then writing the text into the
  882. beginning of the gap, thereby shrinking the gap.  If the gap shrinks
  883. down to nothing, a new gap is created. (What actually happens is that a
  884. new gap is "created" at the end of the buffer's text, which requires
  885. nothing more than changing a couple of indices; then the gap is "moved"
  886. to the position where the insertion needs to take place by moving up in
  887. memory all the text after that position.)  Similarly, deletion occurs
  888. by moving the gap to the place where the text is to be deleted, and
  889. then simply expanding the gap to include the deleted text.
  890. ("Expanding" and "shrinking" the gap as just described means just that
  891. the internal indices that keep track of where the gap is located are
  892. changed.)
  893.  
  894.    Note that the total amount of memory allocated for a buffer text
  895. never decreases while the buffer is live.  Therefore, if you load up a
  896. 20-megabyte file and then delete all but one character, there will be a
  897. 20-megabyte gap, which won't get any smaller (except by inserting
  898. characters back again).  Once the buffer is killed, the memory allocated
  899. for the buffer text will be freed, but it will still be sitting on the
  900. heap, taking up virtual memory, and will not be released back to the
  901. operating system. (However, if you have compiled XEmacs with rel-alloc,
  902. the situation is different.  In this case, the space *will* be released
  903. back to the operating system.  However, this tends to result in a
  904. noticeable speed penalty.)
  905.  
  906.    Astute readers may notice that the text in a buffer is represented as
  907. an array of *bytes*, while (at least in the MULE case) an Emchar is a
  908. 19-bit integer, which clearly cannot fit in a byte.  This means (of
  909. course) that the text in a buffer uses a different representation from
  910. an Emchar: specifically, the 19-bit Emchar becomes a series of one to
  911. four bytes.  The conversion between these two representations is complex
  912. and will be described later.
  913.  
  914.    In the non-MULE case, everything is very simple: An Emchar is an
  915. 8-bit value, which fits neatly into one byte.
  916.  
  917.    If we are given a buffer position and want to retrieve the character
  918. at that position, we need to follow these steps:
  919.  
  920.   1. Pretend there's no gap, and convert the buffer position into a
  921.      "byte index" that indexes to the appropriate byte in the buffer's
  922.      stream of textual bytes.  By convention, byte indices begin at 1,
  923.      just like buffer positions.  In the non-MULE case, byte indices
  924.      and buffer positions are identical, since one character equals one
  925.      byte.
  926.  
  927.   2. Convert the byte index into a "memory index", which takes the gap
  928.      into account.  The memory index is a direct index into the block of
  929.      memory that stores the text of a buffer.  This basically just
  930.      involves checking to see if the byte index is past the gap, and if
  931.      so, adding the size of the gap to it.  By convention, memory
  932.      indices begin at 1, just like buffer positions and byte indices,
  933.      and when referring to the position that is "at" the gap, we always
  934.      use the memory position at the *beginning*, not at the end, of the
  935.      gap.
  936.  
  937.   3. Fetch the appropriate bytes at the determined memory position.
  938.  
  939.   4. Convert these bytes into an Emchar.
  940.  
  941.    In the non-Mule case, (3) and (4) boil down to a simple one-byte
  942. memory access.
  943.  
  944.    Note that we have defined three types of positions in a buffer:
  945.  
  946.   1. "buffer positions" or "character positions", typedef `Bufpos'
  947.  
  948.   2. "byte indices", typedef `Bytind'
  949.  
  950.   3. "memory indices", typedef `Memind'
  951.  
  952.    All three typedefs are just `int's, but defining them this way makes
  953. things a lot clearer.
  954.  
  955.    Most code works with buffer positions.  In particular, all Lisp code
  956. that refers to text in a buffer uses buffer positions.  Lisp code does
  957. not know that byte indices or memory indices exist.
  958.  
  959.    Finally, we have a typedef for the bytes in a buffer.  This is a
  960. `Bufbyte', which is an unsigned char.  Referring to them as Bufbytes
  961. underscores the fact that we are working with a string of bytes in the
  962. internal Emacs buffer representation rather than in one of a number of
  963. possible alternative representations (e.g. EUC-encoded text, etc.).
  964.  
  965. 
  966. File: internals.info,  Node: Buffer Lists,  Next: Markers and Extents,  Prev: The Text in a Buffer,  Up: Buffers and Textual Representation
  967.  
  968. Buffer Lists
  969. ============
  970.  
  971.    Recall earlier that buffers are "permanent" objects, i.e.  that they
  972. remain around until explicitly deleted.  This entails that there is a
  973. list of all the buffers in existence.  This list is actually an
  974. assoc-list (mapping from the buffer's name to the buffer) and is stored
  975. in the global variable `Vbuffer_alist'.
  976.  
  977.    The order of the buffers in the list is important: the buffers are
  978. ordered approximately from most-recently-used to least-recently-used.
  979. Switching to a buffer using `switch-to-buffer', `pop-to-buffer', etc.
  980. and switching windows using `other-window', etc.  usually brings the
  981. new current buffer to the front of the list.  `switch-to-buffer',
  982. `other-buffer', etc. look at the beginning of the list to find an
  983. alternative buffer to suggest.  You can also explicitly move a buffer
  984. to the end of the list using `bury-buffer'.
  985.  
  986.    In addition to the global ordering in `Vbuffer_alist', each frame
  987. has its own ordering of the list.  These lists always contain the same
  988. elements as in `Vbuffer_alist' although possibly in a different order.
  989. `buffer-list' normally returns the list for the selected frame.  This
  990. allows you to work in separate frames without things interfering with
  991. each other.
  992.  
  993.    The standard way to look up a buffer given a name is `get-buffer',
  994. and the standard way to create a new buffer is `get-buffer-create',
  995. which looks up a buffer with a given name, creating a new one if
  996. necessary.  These operations correspond exactly with the symbol
  997. operations `intern-soft' and `intern', respectively.  You can also
  998. force a new buffer to be created using `generate-new-buffer', which
  999. takes a name and (if necessary) makes a unique name from this by
  1000. appending a number, and then creates the buffer.  This is basically
  1001. like the symbol operation `gensym'.
  1002.  
  1003.